package projectServlet;

import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.catalina.Session;

import dao.ConnPool;
import dao.DAOException;
import dao.LeaveDAO;
import dao.LeaveDAOFactory;
import dto.LeaveDTO;
import dto.LeaveSearchDTO;

/**
 * Servlet implementation class leaveServlet
 */
@WebServlet("/leaveServlet")
public class leaveServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public leaveServlet() {
		super();
		// TODO Auto-generated constructor stub

	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub

		try {
			doProcess(request, response);
		} catch (DAOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub

		try {
			doProcess(request, response);
		} catch (DAOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	protected void doProcess(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException,
			DAOException {

		//ConnPool.setupConPool();// Test code! Change this part!!!
		ArrayList<LeaveDTO> Leavereport = new ArrayList<LeaveDTO>();
		// ArrayList<LeaveDTO> Leaver = new ArrayList<LeaveDTO>();
		LeaveDAO leave = LeaveDAOFactory.loadInstance().getLeaveDAO();
		// String leaveType = null;
		String annualButton = request.getParameter("Annual");
		String medicalButton = request.getParameter("Medical");
		String allButton = request.getParameter("All");
		String Print = request.getParameter("Print");
		if (annualButton != null && annualButton.equals("Annual")) {
			request.getSession().setAttribute("LeaveType", annualButton);
		} else if (medicalButton != null && medicalButton.equals("Medical")) {
			request.getSession().setAttribute("LeaveType", medicalButton);
		} else if (allButton != null && allButton.equals("All")) {
			request.getSession().setAttribute("LeaveType", allButton);
		}

		if (request.getParameter("userid") != null
				&& request.getParameter("userid").trim() != "")
			request.getSession().setAttribute("UserID",
					request.getParameter("userid"));

		if (request.getSession().getAttribute("UserID") != null
				&& request.getSession().getAttribute("UserID").toString()
						.trim() != "") {
			
			String leaveType = (String) request.getSession().getAttribute("LeaveType");
			
			String userId = (String) request.getSession()
					.getAttribute("UserID");
			LeaveSearchDTO criti = new LeaveSearchDTO();
			criti.set_userID(Integer.parseInt(userId));
			if(allButton != null && allButton.equals("All")){
				Leavereport=leave.findLeaveByAllSubordinates(Integer.parseInt(userId));
			}else{
			Leavereport = leave.findSubordinatesOnLeave(criti, leaveType);
			}
			request.setAttribute("LeaveInfo", Leavereport);

			RequestDispatcher rd = request.getRequestDispatcher("mReport.jsp");
			rd.forward(request, response);
			if (Print != null && Print.equals("Print")) {
				generateCsvFile("d:\\test.csv", Leavereport);
			}
		} else {
			RequestDispatcher rd = request.getRequestDispatcher("mReport.jsp");
			rd.forward(request, response);
		}

	}

	@SuppressWarnings("unchecked")
	public static void generateCsvFile(String sname,
			ArrayList<LeaveDTO> leaveList) throws IOException {
		try {
			FileWriter fw = new FileWriter(sname);
			fw.append("UserID");
			fw.append(",");
			fw.append("Duration");
			fw.append(",");
			fw.append("Reason");
			fw.append(",");
			fw.append("Status");
			fw.append(",");
			fw.append("ApplicationDate");
			fw.append(",");
			fw.append("RejectReason");
			fw.append('\n');
			for (Iterator<LeaveDTO> i = leaveList.iterator(); i.hasNext();) {
				LeaveDTO item = i.next();
				fw.append(Integer.toString((item.get_userID())));
				fw.append(",");
				fw.append(Integer.toString(item.get_duration()));
				fw.append(",");
				fw.append(item.get_reason());
				fw.append(",");
				fw.append(item.get_status());
				fw.append(",");
				fw.append(item.get_applicationDate().toString());
				fw.append(",");
				fw.append(item.get_rejectReason());
				fw.append('\n');

			}

			fw.flush();
			fw.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}
